package com.weijing.pretty_app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.weijing.pretty_app.R
import com.weijing.pretty_app.databinding.ItemFuSeBinding

class TypeAdapter(private val list: List<Map<String, Int>>) :
    RecyclerView.Adapter<TypeAdapter.TypesHolder>() {

    private var mPosition: Int = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TypesHolder {
        return TypesHolder(
            ItemFuSeBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: TypesHolder, position: Int) {
        val model = list[position]
        model.forEach { (key, value) ->
            with(holder) {

                if (mPosition == position) {
                    binding.itemFuSeTv.setTextColor(
                        ContextCompat.getColor(
                            binding.root.context, R.color.white
                        )
                    )
                    binding.itemFuSeIvTriangle.visibility = View.VISIBLE
                } else {
                    binding.itemFuSeTv.setTextColor(
                        ContextCompat.getColor(
                            binding.root.context, R.color.color_00C1CD
                        )
                    )
                    binding.itemFuSeIvTriangle.visibility = View.INVISIBLE
                }
                binding.itemFuSeTv.text = key
                Glide.with(binding.root.context).load(value).into(binding.itemFuSeIv)
                if (onClickListener != null) {
                    holder.itemView.setOnClickListener {
                        onClickListener?.onItemClickListener(position, key)
                    }
                }
            }
        }
    }

    class TypesHolder(val binding: ItemFuSeBinding) : RecyclerView.ViewHolder(binding.root)

    var onClickListener: OnClickListener? = null

    interface OnClickListener {
        fun onItemClickListener(position: Int, type: String)
    }

    fun setIndex(position: Int) {
        mPosition = position
        notifyDataSetChanged()
    }
}